<?php

/*
 * 摇一摇页面主入口文件
 */
namespace Huodong\Controller;

use Library\Wx;
use Huodong\Controller\CommonController;
use Library\Prizes\Prizes;
use Library\CardCode;
use Library\PrizesOrder;
use Library\User;
use Think\Log;

/**
 * 首页
 */
class IndexController extends CommonController {
    /*
     *  限制从从微信进入
     */
    private function checkSource() {
        if ($this->isWeixin() == false) {
            $this->assign('isWX', 1);
            $this->assign('info', '请使用微信扫码进入,或者使用微信打开链接。');
            $this->display(':tips');
            exit;
        } else {
            $this->weixin_auth();
        }
    }

    //首页
    public function dhxy3() {
        $this->statistics_log();

        if (isset($_SESSION['isFirst']) && isset($_SESSION['open_id'])) {
            $_SESSION['isFirst'] = false;
        } else {
            $_SESSION['isFirst'] = true;
            $_SESSION['open_id'] = 'tmp'.mt_rand(1000000, 9900000);
        }

        $shake = new \Library\HuodongShake(2);
        $event = $shake->get_activity_by_pageId();

        if ($event) {
            $pageinfo['bg_pic'] = $event['bg_pic'] != '' ? C('IMG_PATH').$event['bg_pic'] : '';
            $pageinfo['center_pic'] = $event['center_pic'];
            $pageinfo['event_intro'] = $event['event_intro'];
            $this->assign('pageinfo', $pageinfo);
        } else {
            Log::record('暂时没有活动');
            $this->assign('info', '暂时没有活动');
            $this->display(':tips');
            exit;
        }
        
        if (isset($_SESSION['isFirst']) && isset($_SESSION['open_id'])) {
            $_SESSION['isFirst'] = false;
        } else {
            $_SESSION['isFirst'] = true;
            $_SESSION['open_id'] = 'tmp'.mt_rand(1000000, 9900000);
        }

        try {
            $shake = new \Library\HuodongShake(2);
            $prizes = $shake->get_prizes();
        } catch (\Exception $e) {
            \Think\Log::record('shake catch Exception'.$e->getMessage());
        }
        if ($prizes) {
            $desc = $prizes->get_view_desc();
            $description = $desc['description'];
            $_SESSION['card_code'] = $desc['card_code'];
            $_SESSION['prizes_grade'] = $desc['prizes_grade'];
            Log::record(json_encode($desc));
            Log::record(($_SESSION['card_code']));
        }
        $this->assign('prizes_grade', $desc['prizes_grade']);
        $this->assign('description', $description);
       /* $this->assign('first_visit', $_SESSION['isFirst']);*/
        $this->display(":dhxy3");
    }

    //首页
    public function index() {
        $this->statistics_log();
        $this->checkSource();
        $this->display(":olympic_end");exit;

        $shake = new \Library\HuodongShake(2);
        $event = $shake->get_activity_by_pageId();

        if ($event) {
            $pageinfo['bg_pic'] = $event['bg_pic'] != '' ? C('IMG_PATH').$event['bg_pic'] : '';
            $pageinfo['center_pic'] = $event['center_pic'];
            $pageinfo['event_intro'] = $event['event_intro'];
            $this->assign('pageinfo', $pageinfo);
        } else {
            Log::record('暂时没有活动');
            $this->assign('info', '暂时没有活动');
            $this->display(':tips');
            exit;
        }

        Log::record('isFirst:'.$shake->isFirst());
        $this->assign('first_visit', $shake->isFirst());

        $user_wallet = new \Library\UserWallet();
        $money = $user_wallet->get_user_money();
        $this->assign('money', $money / 100);
        $this->assign('user_info', $_SESSION['wxuser']);
        $this->addJsSdk();
        $this->display(":olympic");
    }

    //个人账户中心
/*    public function honghongbao() {
        $this->checkSource();

        $user_wallet = new \Library\UserWallet();
        $money = $user_wallet->get_user_money();

        $cardCode = CardCode::getInstance();
        $userCards = $cardCode->getAllUserCard();

        $this->assign('user_info', $_SESSION['wxuser']);
        $this->assign('money', $money / 100);
        $this->assign('userCards', $userCards);
        $this->display(":honghongbao");

        $this->statistics_log();
    }*/
    public function honghongbao() {
        $mobile = I('post.mobile', '');
        if (!$mobile) {
            $this->display(':mycard_verif');
            exit;
        }
        $userid = $_SESSION['user_id'];

        if (!$userid) {
            $user = User::getInstance();
            $userid = $user->getUseridbymobile($mobile);
        }
        if ($userid) {
            $prizesOrder = new PrizesOrder();
            $prizes = $prizesOrder->getOneCardByUserd($userid);
            if ($prizes) {
                $cardCode = CardCode::getInstance();
                $cardDetail = $cardCode->getCardDetail($prizes['card_id'], $prizes['card_num']);

                if ($cardDetail) {
                    $this->assign('cardDetail', $cardDetail);
                    $this->display(':mycard_detail');
                } else {
                    $this->assign('info', '无效的卡券');
                    $this->display(':tips');
                }
                exit;
            }
        }
        $this->assign('info', '没有奖品。');
        $this->display(':tips');
    }
    public function cardCenter() {

    }
    /*
     * 卡券详情页面
     */
    public function cardDetail() {
        $this->checkSource();

        $code = $_GET['cardCode'];
        $cardId = $_GET['cardId'];

        $cardCode = CardCode::getInstance();
        $cardDetail = $cardCode->getCardDetail($cardId, $code);

        if ($cardDetail) {
            $this->assign('cardDetail', $cardDetail);
            $this->display(':mycard_detail');
        } else {
            $this->assign('info', '无效的卡券');
            $this->display(':tips');
        }
        $this->statistics_log();
    }

    /*
     *  页面点击统计
     */
    public function analytics() {
        $this->statistics_log();
        $real_url = $_GET['real_url'];
        redirect($real_url);
    }

    /**
     * @param url 要生成的二维码的链接
     * @param flag 是否生成图片保存起来，1是，0否（只是动态生成）
     */
    public function createQRcode() {
        $url = $_GET['url'];
        createQRcode($url, 0);
    }

    /*
     * H5 核销卡券页面
     */
    public function  verification() {
        $code = $_GET['card_num'];
        $cardId = $_GET['card_id'];

        $cardCode = CardCode::getInstance();
        $cardDetail = $cardCode->getCardDetail($cardId, $code);

        if ($cardDetail) {
            $this->assign('cardDetail', $cardDetail);
        }

        $this->display(':verification');
    }

    private function addJsSdk() {
        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $js_sign = Wx::getJsSign($url);

        $jscard_sign = Wx::getJsCardSign($arg);

        $this->assign('cardid', 'p9Vj9vgCSAR6d8HnhrzDV8AAHeI4');
        $this->assign('js_sign', $js_sign);
        $this->assign('jscard_sign', $jscard_sign);
    }

    private function getOpenid() {
        $openid = $_SESSION['open_id'];

        if (!empty($openid)) {
            $this->assign('openid', $openid);
        } else {
            $this->assign('openid', '');
        }
    }
}
